define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'modify/order/index' + location.search,
                    add_url: 'modify/order/add',
                    // edit_url: 'modify/order/edit',
                    // del_url: 'modify/order/del',
                    multi_url: 'modify/order/multi',
                    import_url: 'modify/order/import',
                    table: 'modify_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'sn', title: __('Sn'), operate: 'LIKE' },
                        { field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE' },
                        { field: 'title', title: __('Title'), operate: 'LIKE' },
                        { field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'num', title: __('Num') },
                        { field: 'pay_price', title: __('Pay_price'), operate: 'BETWEEN' },

                        { field: 'pay_type', title: __('Pay_type'), searchList: { "1": __('Pay_type 1') }, formatter: Table.api.formatter.normal },
                        { field: 'contact', title: __('Contact'), operate: 'LIKE' },
                        { field: 'telephone', title: __('Telephone'), operate: 'LIKE' },
                        { field: 'user_address.address', title: __('收货地址'), operate: 'LIKE' },
                        { field: 'deliver', title: __('Deliver'), searchList: { "1": __('Deliver 1'), "2": __('Deliver 2') }, formatter: Table.api.formatter.normal },
                        { field: 'address.title', title: __('服务点名称'), operate: 'LIKE' },
                        { field: 'address.address', title: __('服务点地址'), operate: 'LIKE' },
                        { field: 'status', title: __('Status'), searchList: { '0': __('Status 0'), '1': __('Status 1'), '2': __('Status 2'), '3': __('Status 3'), '4': __('Status 4'), '-1': __('Status -1') }, formatter: Table.api.formatter.normal },
                        { field: 'cancel_status', title: __('退款状态'), searchList: { '0': __('未申请'), '1': __('待审核'), '2': __('已审核') }, formatter: Table.api.formatter.normal },
                        { field: 'mark', title: __('Mark'), operate: 'LIKE' },
                        { field: 'paytime', title: __('Paytime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'query',
                                    title: '查看物流',
                                    text: '查看物流',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'express/query?express_com={express}&express_no={express_sn}',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.status >= '2') {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }

                                    },
                                },
                                {
                                    name: 'deliver',
                                    title: '发货',
                                    text: '发货',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'modify/order/deliver',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.status == '1' && row.deliver == '1') {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }
                                    },
                                },
                                {
                                    name: 'refund',
                                    title: '退款',
                                    text: '退款',
                                    confirm: '确认退款?',
                                    classname: 'btn btn-primary btn-xs btn-view btn-ajax',
                                    url: 'modify/order/refund',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.cancel_status == '1' && row.status < '3') {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }
                                    },
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        deliver: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
